package com.cincc.cinccimdemo.ui.contact;

import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.RecyclerView;

import com.cincc.cinccimdemo.R;
import com.cincc.cinccimdemo.adapter.CreateGroupAdapter;
import com.cincc.cinccimdemo.common.Constants;
import com.cincc.cinccimdemo.common.ContactUtil;
import com.cincc.cinccimdemo.common.callback.OnItemClickListener;
import com.cincc.cinccimdemo.model.FriendInfoUI;
import com.cincc.cinccimdemo.ui.IMBaseActivity;
import com.cincc.cinccimdemo.ui.conversation.ConversationActivity;
import com.cincc.cinccimdemo.ui.conversation.member.AddMemberActivity;
import com.cincc.cinccimdemo.viewmodel.ContactViewModel;
import com.cincc.cinccimdemo.widget.SideBarView;
import com.cincc.im.core.IMSipPhoneCtrl;
import com.cincc.im.model.enums.ImSendType;

import java.util.ArrayList;
import java.util.List;

public class CreateGroupActivity extends IMBaseActivity {
    @Override
    protected int contentLayout() {
        return R.layout.activity_create_group;
    }

    RecyclerView recyclerView;
    SideBarView sideBarView;
    TextView mTextCenterContact;
    CreateGroupAdapter createGroupAdapter;
    ContactViewModel contactViewModel;
    private MenuItem menuItem;
    private TextView confirmTv;

    @Override
    protected void bindViews() {
        super.bindViews();
        recyclerView = findViewById(R.id.mList_contact);
        sideBarView = findViewById(R.id.mSide_bar_contact);
        mTextCenterContact = findViewById(R.id.mText_center_contact);
    }

    @Override
    protected void afterViews() {
        super.afterViews();
        createGroupAdapter = new CreateGroupAdapter(this);
        recyclerView.setAdapter(createGroupAdapter);
        contactViewModel = new ViewModelProvider(this).get(ContactViewModel.class);
        contactViewModel.contactListLiveData().observe(this, friendInfos -> {
            createGroupAdapter.setContactList(friendInfos);
            setSideView();
        });
        createGroupAdapter.setOnDataClickItem(new OnItemClickListener() {
            @Override
            public void onClick(int index) {
                createGroupAdapter.updateItem(index);
                List<FriendInfoUI> checkedList = createGroupAdapter.getCheckedList();
                if (!checkedList.isEmpty()) {
                    confirmTv.setText("完成(" + checkedList.size() + ")");
                    menuItem.setEnabled(true);
                } else {
                    confirmTv.setText("完成");
                    menuItem.setEnabled(false);
                }
            }
        });
    }

    @Override
    protected int menu() {
        return R.menu.contact_pick;
    }

    @Override
    protected void afterMenus(Menu menu) {
        menuItem = menu.findItem(R.id.confirm);
        menuItem.setEnabled(false);
    }

    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        View view = menuItem.getActionView();
        confirmTv = view.findViewById(R.id.confirm_tv);
        confirmTv.setOnClickListener(v -> onOptionsItemSelected(menuItem));
        return super.onPrepareOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == R.id.confirm) {
            onConfirmClick();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }


    private void onConfirmClick() {
        List<FriendInfoUI> checkedList = createGroupAdapter.getCheckedList();
        String groupName = Constants.userInfo.nickName + "," + checkedList.get(0).nickName;
        List<String> uids = new ArrayList<>();
        for (FriendInfoUI infoUI : checkedList) {
            uids.add(infoUI.userId);
        }
        IMSipPhoneCtrl.getInstance().createGroup(groupName, uids, imResponse -> {
            if (imResponse.getCode() == 0) {
                String groupId = (String) imResponse.getData();
                ConversationActivity.startGroupConversation(CreateGroupActivity.this, groupId, groupName);
                finish();
            } else {
                runOnUiThread(() -> Toast.makeText(CreateGroupActivity.this, imResponse.getMsg(), Toast.LENGTH_SHORT).show());
            }
        });
    }

    public void setSideView() {
        // 设置需要显示的索引栏内容
        String[] letters = ContactUtil.getLetters(createGroupAdapter.getmList());
        sideBarView.setLetter(letters);
        // 设置需要显示的提示框
        sideBarView.setTextView(mTextCenterContact);
        sideBarView.setOnTouchingLetterChangedListener(s -> {
            int position = ContactUtil.getLetterPosition(createGroupAdapter.getmList(), s);
            if (position != -1) {
                recyclerView.scrollToPosition(position);
            }
        });
    }
}
